/*
 * Copyright 2006 Abdulla G. Abdurakhmanov (abdulla.abdurakhmanov@gmail.com).
 * 
 * Licensed under the GPL, Version 2 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.gnu.org/copyleft/gpl.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * With any your questions welcome to my e-mail 
 * or blog at http://abdulla-a.blogspot.com.
 */

package org.ofsm.creator;

import java.awt.Color;

import java.awt.Font;

import java.awt.Point;
import java.awt.geom.Point2D;

import java.util.Collection;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;

import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;

import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.ConnectionSet;

import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;

import org.jgraph.graph.GraphConstants;

import org.ofsm.IState;
import org.ofsm.impl.FileFiniteStateMachineModel;

public class DiagramGraphCreationContext {
    private Collection<IState> alreadyCheckedStates = new LinkedList<IState> ();
    private ConnectionSet cs = new ConnectionSet();
    private Map<DefaultGraphCell,AttributeMap> attributes = new Hashtable<DefaultGraphCell,AttributeMap>();
    private List<Object> graphItems = new LinkedList<Object> ();
    private Point maxUsedCoords = new Point();        
    
    public DiagramGraphCreationContext() {
    }

    public Collection<IState> getAlreadyCheckedStates() {
        return alreadyCheckedStates;
    }
    
    public ConnectionSet getConnectionSet() {
        return cs;
    }

    public Map<DefaultGraphCell,AttributeMap> getAttributes() {
        return attributes;
    }

    public List<Object> getGraphItems() {
        return graphItems;
    }
    
    public void addCell(DefaultGraphCell cell, FileFiniteStateMachineModel.StateInfo state) {
        getGraphItems().add(cell);
    }
    
    public void addEdge(DefaultGraphCell fromCell, DefaultGraphCell toCell, DefaultEdge edge) {
        getGraphItems().add(edge);
    }

    public AttributeMap createBounds(AttributeMap map, int x, int y, Color c) {
            GraphConstants.setBounds(map, map.createRect(x, y, 90, 30));
            GraphConstants.setBorder(map, BorderFactory.createRaisedBevelBorder());
            GraphConstants.setBackground(map, c.darker().darker());
            GraphConstants.setGradientColor(map, c.brighter().brighter().brighter());
            GraphConstants.setForeground(map, Color.white);
            if (GraphConstants.DEFAULTFONT != null) {
                    GraphConstants.setFont(map, GraphConstants.DEFAULTFONT.deriveFont(
                                    Font.BOLD, 12));
            }
            GraphConstants.setOpaque(map, true);
            return map;
    }
    
    public AttributeMap createArrowStyle(AttributeMap arrowStyle, Color color, boolean biDirectional) {
        GraphConstants.setLineEnd(arrowStyle,
                        GraphConstants.ARROW_TECHNICAL);
        if(!biDirectional) {
            GraphConstants.setLineBegin(arrowStyle,
                            GraphConstants.ARROW_TECHNICAL);            
        }
        GraphConstants.setBeginSize(arrowStyle, 10);
        GraphConstants.setDashPattern(arrowStyle, new float[] { 3, 3 });
        GraphConstants.setForeground(arrowStyle, color);
        GraphConstants.setLineColor( arrowStyle, color);
        GraphConstants.setDisconnectable(arrowStyle, false);
        if (GraphConstants.DEFAULTFONT != null) {
                GraphConstants.setFont(arrowStyle, GraphConstants.DEFAULTFONT.deriveFont(10));
        }
        return arrowStyle;
    }

    public Point getMaxUsedCoords() {
        return maxUsedCoords;
    }
}
